class Solution {
public:
    bool check(TreeNode* A,TreeNode* B){
        if(!B){
            return true;
        }
        if(B && !A || A->val != B->val){
            return false;
        }
        bool left = check(A->left,B->left);
        bool right = check(A->right,B->right);
        if(left && right){
            return true;
        }
        return false;
    }
                                        
    bool isSubStructure(TreeNode* A, TreeNode* B) {
        if(B && !A || A && !B){
            return false;
        }
        if(!B && !A){
            return true;
        }
        
        if(A->val == B->val){
            if(check(A,B)){
                return true;
            }
        }
        
        if(isSubStructure(A->left,B)){
            return true;
        }
        if(isSubStructure(A->right,B)){
            return true;
        }
        
        return false;
    }
};
